﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.XtraRichEdit.Model;

namespace SpreadSheetFramework.Util
{
    public class ObjInputRowRange
    {
        public string Name { get; set; }
        public int LeftColIndex { get; set; }
        public int RightColIndex { get; set; }
        public int TopRowIndex { get; set; }
        public int BottomRowIndex { get; set; }
        public string SheetName { get; set; }
        public bool AllowInsertDelete { get; set; }

        public ObjInputRowRange(string name, string sheetName, int topRow, int bottomRow, int leftColIndex, int rightColIndex, bool allowInsertDelete)
        {
            Name = name;
            SheetName = sheetName;
            TopRowIndex = topRow;
            BottomRowIndex = bottomRow;
            AllowInsertDelete = allowInsertDelete;
            LeftColIndex = leftColIndex;
            RightColIndex = rightColIndex;
        }


        public bool IsInsertRow(int index, string sheetName)
        {
            return AllowInsertDelete && Contain(index, sheetName);
        }

        public bool Contain(int index, string sheetName)
        {
            return index >= TopRowIndex && index <= BottomRowIndex && sheetName == SheetName;
        }


        public void UpdateInsert(int index, string sheetName)
        {
            if(sheetName!=SheetName) return;
            if (index < TopRowIndex)
            {
                TopRowIndex++;
                BottomRowIndex++;
            }
            else if (index >= TopRowIndex && index <= BottomRowIndex)
            {
                BottomRowIndex++;
            }
        }

        public void UpdateDelete(int index, string sheetName)
        {
            if (sheetName != SheetName) return;
            if (index < TopRowIndex)
            {
                TopRowIndex--;
                BottomRowIndex--;
            }
            else if (index >= TopRowIndex && index <= BottomRowIndex)
            {
                BottomRowIndex--;
            }
        }

        public bool IsDeleteRow(int index, string sheetName)
        {
            return AllowInsertDelete && Contain(index, sheetName) && BottomRowIndex != TopRowIndex;
        }
    }
}